package com.thread.www;

import com.google.common.collect.Maps;

import java.util.Map;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * Created by 27340 on 2017/3/21.
 */
public class ThreadLockTest1 {
    private final Map<String , Object> map = Maps.newHashMap();
    private final ReentrantReadWriteLock rwLock = new ReentrantReadWriteLock();
    public Object readWrite(String id){
        Object value = null;
        rwLock.readLock().lock();
        try {
            value = map.get(id);
            if(value==null){
                rwLock.readLock().unlock();
                rwLock.writeLock().lock();
                try {
                    if(value==null){
                        value="aaa";
                    }
                }finally {
                    rwLock.writeLock().unlock();
                }
                rwLock.readLock();
            }
        }finally {
            rwLock.readLock().unlock();
        }
        return value;
    }
}
